最佳实践
matt大神在这里给出了一个最佳实践:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21extension UIViewController {
    private struct AssociatedKeys {
        static var DescriptiveName = "nsh_DescriptiveName"
    }
    var descriptiveName: String? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.DescriptiveName) as? String
        }
        set {
            if let newValue = newValue {
                objc_setAssociatedObject(
                    self,
                    &AssociatedKeys.DescriptiveName,
                    newValue as NSString?,
                    UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                )
            }
        }
    }
}
一个利用runtime添加角标的例子
1  | //用于显示小角标  | 
用法:1
2var btn = UIButton()
btn.showCornerStatus(8, rateX: 0.7, rateY: 0.12)
效果: